export const validatNumber = name => (rule, value, callback) => {
    const reg = /^(([1-9]{1}\d*)|(0{1}))(\.\d{0,2})?$/;
    value = (value || '0').toString();
    if (value && value.slice(0, 1) === '-') {
        value = value.slice(1);
    }

    if (!value || (value && reg.test(value))) {
        callback();
    } else {
        return callback(new Error(name + '应为0-2位小数的数字'));
    }
};

export const validatString = count => (rule, value, callback) => {
    value = (value || '').toString();

    if (value.length >= 0 && value.length <= count) {
        callback();
    } else {
        return callback(new Error(`长度限制为${count}个字符`));
    }
};

export default function getRules(options = {}) {
    const RULES = {
        documentNo: [
            {required: true, message: '单据编号必填项', trigger: ['change', 'blur']},
            {validator: validatString(30), trigger: ['change', 'blur']}
        ],
        documentDate: [{required: true, message: '单据日期必填项', trigger: ['change', 'blur']}],
        customer: [
            {required: true, message: '客户必填项', trigger: ['change', 'blur']},
            {validator: validatString(30), trigger: ['change', 'blur']}
        ],
        vendor: [
            {required: true, message: '供应商必填项', trigger: ['change', 'blur']},
            {validator: validatString(30), trigger: ['change', 'blur']}
        ],
        person: [
            {required: true, message: '财务必填项', trigger: ['change', 'blur']},
            {validator: validatString(30), trigger: ['change', 'blur']}
        ],
        account: [
            {required: true, message: '账号必填项', trigger: ['change', 'blur']},
            {validator: validatString(30), trigger: ['change', 'blur']}
        ],
        depot: [
            {required: true, message: '仓库必填项', trigger: ['change', 'blur']},
            {validator: validatString(30), trigger: ['change', 'blur']}
        ],
        total: [
            // {required: true, message: '收入金额必填项', trigger: ['change', 'blur']},
            // {validator: validatNumber('收入金额'), trigger: ['change', 'blur']}
        ],
        remark: [{validator: validatString(200), trigger: ['change', 'blur']}],
        date: [{required: true, message: '日期必填项', trigger: ['change', 'blur']}],
        itemOutInId: [
            {required: true, message: '收支项目必填项', trigger: ['change', 'blur']},
            {validator: validatString(30), trigger: ['change', 'blur']}
        ],
        money: [
            {required: true, message: '金额必填项', trigger: ['change', 'blur']},
            {validator: validatNumber('金额'), trigger: ['change', 'blur']}
        ],
        abstract: [
            {required: true, message: '摘要必填项', trigger: ['change', 'blur']},
            {validator: validatString(200), trigger: ['change', 'blur']}
        ]
    };

    return Object.assign(RULES, options);
}
export async function formValidates(values, refs) {
    const $refs = refs || this.$refs;
    const handers = [];
    values.forEach(item => {
        const uuid = item.uuid;
        const formNames = [
            `form_documentNo_${uuid}`,
            `form_documentDate_${uuid}`,
            `form_depot_${uuid}`,
            `form_customer_${uuid}`,
            `form_vendor_${uuid}`,
            `form_person_${uuid}`,
            `form_account_${uuid}`,
            `form_total_${uuid}`,
            `form_remark_${uuid}`,
            `form_itemOutIns_date_${uuid}`,
            `form_itemOutIns_itemOutInId_${uuid}`,
            `form_itemOutIns_money_${uuid}`,
            `form_itemOutIns_abstract_${uuid}`
        ];
        formNames.forEach(formName => {
            if (!$refs[formName]) {
                console.warn('formName', formName);
                return;
            }
            const hander = new Promise((resolve, reject) => {
                $refs[formName].validate((valid, fields) => {
                    if (valid) {
                        resolve(valid);
                        return;
                    }
                    reject(valid);
                });
            });
            handers.push(hander);
        });
    });
    await Promise.all(handers);
}
